package com.xindaoapp.happypet.activity;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.xindaoapp.happypet.R;

/**
 * 加载WebView
 * 
 * @author
 * 
 */
public class WebInfoActivity extends UMBaseActivity implements OnClickListener {
    @ViewInject(R.id.tittle_shop_cancel)
    private ImageView tittle_shop_cancel;
    @ViewInject(R.id.tittle_shop_content)
    private TextView tittle_shop_content;
    private String url;

    @ViewInject(R.id.webview)
    private WebView webview;

    @ViewInject(R.id.bottom_left)
    private ImageView bottom_left;
    @ViewInject(R.id.bottom_right)
    private ImageView bottom_right;
    @ViewInject(R.id.bottom_refresh)
    private ImageView bottom_refresh;
    @ViewInject(R.id.bottom_ie)
    private ImageView bottom_ie;
    private String name;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        url = getIntent().getStringExtra("url");
        name = getIntent().getStringExtra("name");
        setContentView(R.layout.activity_webinfo);
        ViewUtils.inject(this);
        initClick();
    }

    private void initClick() {
        tittle_shop_cancel.setOnClickListener(this);
        bottom_left.setOnClickListener(this);
        bottom_right.setOnClickListener(this);
        bottom_refresh.setOnClickListener(this);
        bottom_ie.setOnClickListener(this);
        tittle_shop_content.setText(name);
        webview.getSettings().setJavaScriptEnabled(true);
        webview.getSettings().setDefaultTextEncodingName("UTF-8");
        // 支持缩放
        webview.getSettings().setSupportZoom(true);
        // 缩放按钮
        webview.getSettings().setBuiltInZoomControls(true);
        webview.getSettings().setUseWideViewPort(true);
        webview.getSettings().setLoadWithOverviewMode(true);
        webview.loadUrl(url);
        // webview.loadDataWithBaseURL(null,url, "text/html", "utf-8",null);
        webview.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String l) {
                if (l != null) {
                    webview.loadUrl(l);
                    url = l;
                }
                return true;
            };
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tittle_shop_cancel:
                finish();
                break;
            case R.id.bottom_left:
                if (webview.canGoBack()) {
                    webview.goBack();
                } else {
                    Toast.makeText(getApplicationContext(), "没有上一页", Toast.LENGTH_SHORT).show();
                }
                break;
            case R.id.bottom_right:
                if (webview.canGoForward()) {
                    webview.canGoForward();
                } else {
                    Toast.makeText(getApplicationContext(), "没有下一页", Toast.LENGTH_SHORT).show();
                }
                break;
            case R.id.bottom_refresh:
                webview.reload();
                break;
            case R.id.bottom_ie:
                Intent intent = new Intent("android.intent.action.VIEW");
                intent.setData(Uri.parse(url));
                startActivity(intent);
                break;
            default:
                break;
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if ((keyCode == KeyEvent.KEYCODE_BACK) && webview.canGoBack()) {
            webview.goBack();
            return true;
        } else {
            finish();
        }
        return super.onKeyDown(keyCode, event);
    }

}
